package com.yuji.aacar.widgets.dialogue;

import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.support.annotation.NonNull;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.View;
import android.view.ViewTreeObserver;
import android.view.Window;
import android.view.WindowManager;
import android.widget.TextView;
import android.widget.Toast;

import com.yuji.aacar.R;
import com.yuji.aacar.bean.response.VersionResponse;
import com.yuji.aacar.core.cores.utils.LogUtil;
import com.yuji.aacar.service.DownloadService;
import com.yuji.aacar.tools.Tools;
import com.yuji.aacar.widgets.autolayout.AutoLinearLayout;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * <li>Package:com.yuji.aacar.widgets.dialogue</li>
 * <li>Author: lining </li>
 * <li>Date: 2018/3/29</li>
 * <li>Description:   </li>
 */

public class VersionDialog extends Dialog {

    @BindView(R.id.dialog_cancel)
    TextView dialogCancel;
    @BindView(R.id.dialog_confirm)
    TextView dialogConfirm;
    @BindView(R.id.select_layout)
    AutoLinearLayout selectLayout;
    @BindView(R.id.dialog_update)
    TextView dialogUpdate;
    @BindView(R.id.must_update_layout)
    AutoLinearLayout mustUpdateLayout;
    @BindView(R.id.version)
    TextView version;
    @BindView(R.id.tips_tv)
    TextView tipsTv;
    private Context mContext;
    private VersionResponse mVersionInfo;

    public VersionDialog(@NonNull Context context) {
        super(context);
    }


    public VersionDialog(@NonNull Context context, VersionResponse mVersionInfo) {
        super(context);
        mContext = context;
        setCancelable(false);
        Window window = getWindow();
        window.requestFeature(Window.FEATURE_NO_TITLE);
        WindowManager.LayoutParams wAttrs = window.getAttributes();
        wAttrs.gravity = Gravity.CENTER;
        window.setBackgroundDrawable(mContext.getResources().getDrawable((android.R.color.transparent)));
        getWindow().getDecorView().getViewTreeObserver().addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() {
            @Override
            public boolean onPreDraw() {
                int height = getWindow().getDecorView().getMeasuredHeight();
                int width = getWindow().getDecorView().getMeasuredWidth();
                getWindow().setLayout(width, height);
                getWindow().getDecorView().getViewTreeObserver().removeOnPreDrawListener(this);
                return false;
            }
        });


        this.mVersionInfo = mVersionInfo;
        setContentView(R.layout.dialog_version);
        ButterKnife.bind(this);
        this.mContext = context;
//        version.setText(mContext.getResources().getString(R.string.dialog_version_update_title, mVersionInfo.getVersion()));
        tipsTv.setText(mVersionInfo.getDetail());
        Window window1 = getWindow();
        WindowManager.LayoutParams attributes = window1.getAttributes();
        attributes.width = (int) (Tools.getDisplayWidth(mContext) * 0.8);
        window1.setAttributes(attributes);
    }

    public void showDialog() {
        if (mVersionInfo.getForce().contentEquals("1")) {
            setCancelable(false);
            mustUpdateLayout.setVisibility(View.VISIBLE);
            LogUtil.e("CoreEventBusMessage", "LEVEL_MUST");
//            CorePersistenceUtil.saveObject("versionJson", "");
            show();
        } else {
            setCancelable(true);
            mustUpdateLayout.setVisibility(View.VISIBLE);
            LogUtil.e("CoreEventBusMessage", "LEVEL_RECOMMEND");
//            CorePersistenceUtil.saveObject("versionJson", "");
            show();
        }
    }


    @OnClick({R.id.dialog_cancel, R.id.dialog_confirm, R.id.dialog_update})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.dialog_cancel:
                dismiss();
                break;
            case R.id.dialog_confirm:
                startDownload();
                dismiss();
                break;
            case R.id.dialog_update:
                startDownload();
                break;
        }
    }


    /**
     * 开始下载
     */
    private void startDownload() {
        String path = mContext.getExternalCacheDir().getAbsolutePath();
        try {
            //系统的浏览器下载
            if (TextUtils.isEmpty(path)) {
                startDownSystem();
            } else {
                //自定义的后台下载
                startDownMy(path);
            }
        } catch (Exception e) {
            e.printStackTrace();
            startDownSystem();
        }
    }

    /**
     * 系统浏览器下载
     */
    private void startDownSystem() {
        Uri uri = Uri.parse(mVersionInfo.getUrl());
        Intent it = new Intent(Intent.ACTION_VIEW, uri);
        mContext.startActivity(it);
    }

    /**
     * 自定义下载
     */
    private void startDownMy(String path) {
        //自定义的后台下载
        LogUtil.e("startDownMy path", path);
        LogUtil.e("startDownMy url", mVersionInfo.getUrl());
        Intent intent = new Intent(mContext, DownloadService.class);
        intent.putExtra("url", mVersionInfo.getUrl());
        intent.putExtra("path", path + "/aacar.apk");
        intent.setPackage(mContext.getPackageName());
        LogUtil.e("startDownMy", mContext);
        mContext.startService(intent);

        Toast.makeText(mContext, "开始下载", Toast.LENGTH_LONG).show();
    }

}
